package com.dd.keel.core.common.utils;

/**
 * 循环计数器
 * @author Kevin.XU
 *
 */
public class CycleCounter {
	
	private long minValue = 0;
	private long maxValue = 0;
	private long curValue = 0;
	
	
	public CycleCounter(long min_value,long max_value)
	{
		reset(min_value,max_value);
	}
	
	public long getMinValue() {
		return minValue;
	}

	public void setMinValue(long minValue) {
		this.minValue = minValue;
	}
	
	public long getMaxValue() {
		return maxValue;
	}
	
	public void setMaxValue(long maxValue) {
		this.maxValue = maxValue;
	}
	
	public void invalid()
	{
		reset(-1,-1);
	}
	
	public boolean isInvalid()
	{
		return minValue == -1 && maxValue == -1;
	}
	
	public void reset(long min_value,long max_value)
	{
		this.minValue = min_value;
		this.maxValue = max_value;
		if(minValue>-1 && maxValue>=minValue)
		{
			curValue = minValue-1;
		}
	}
	
	/**
	 * 线程不安全的next方法
	 * @return
	 */
	public long ns_next()
	{
		if(++curValue>maxValue)
		{
			curValue = minValue;
		}
		return curValue;
	}
	
	/**
	 * 线程安全的next方法
	 * @return
	 */
	public synchronized long next()
	{
		if(++curValue>maxValue)
		{
			curValue = minValue;
		}
		return curValue;
	}
	
	public long curr()
	{
		return curValue;
	}
	
	public String toString()
	{
		return "curent is "+curValue+", minValue is "+minValue+", maxValue is "+maxValue;
	}
	
}
